home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / TSFAQP18.ZIP / FAQPAS3.TXT < prev    next >
Text File  |  1993-12-26  |  11KB  |  305 lines

  1. From ts@uwasa.fi Sun Dec 26 00:00:00 1993
  2. Subject: FAQPAS3.TXT contents
  3.                                   Copyright (c) 1993 by Timo Salmi
  4.                                                All rights reserved
  5.  
  6. FAQPAS3.TXT The third set of frequently (and not so frequently)
  7. asked Turbo Pascal questions with Timo's answers. The items are in
  8. no particular order.
  9.  
  10. You are free to quote brief passages from this file provided you
  11. clearly indicate the source with a proper acknowledgment.
  12.  
  13. Comments and corrections are solicited. But if you wish to have
  14. individual Turbo Pascal consultation, please rather post your
  15. questions to a suitable UseNet newsgroup like comp.lang.pascal. It
  16. is much more efficient than asking me by email. I'd like to help,
  17. but I am very pressed for time. I prefer to pick the questions I
  18. answer from the Usenet news. Thus I can answer publicly at one go if
  19. I happen to have an answer. Besides, newsgroups have a number of
  20. readers who might know a better or an alternative answer. Don't be
  21. discouraged, though, if you get a reply like this from me. I am
  22. always glad to hear from fellow Turbo Pascal users.
  23.  
  24. ..................................................................
  25. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  26. Moderating at garbo.uwasa.fi anonymous FTP  archives  128.214.87.1
  27. Faculty of Accounting & Industrial Management; University of Vaasa
  28. Internet: ts@uwasa.fi   BBS +(358)-61-3170972; FIN-65101,  Finland
  29.  
  30. -------------------------------------------------------------------
  31. 61) What are Binary Coded Decimals? How to convert them?
  32. 62) How can I copy a file in a Turbo Pascal program?
  33. 63) How can I use C code in my Turbo Pascal program?
  34. 64) How do I get started with the Turbo Profiler?
  35. -------------------------------------------------------------------
  36.  
  37. From ts@uwasa.fi Sun Dec 26 00:01:01 1993
  38. Subject: Binary Coded Decimals
  39.  
  40. 61. *****
  41.  Q: What are Binary Coded Decimals? How to convert them?
  42.  
  43.  A: Let us look at full integers only and skip the even more
  44. difficult question of BCD reals and BCD operations.
  45.      Decimal Hexa  BCD
  46.         1      $1    1
  47.         :      $9    9
  48.        10      $A   ..
  49.         :       :    :
  50.        12      $C   ..
  51.         :       :    :
  52.        16     $10   10
  53.        17     $11   11
  54.        18     $12   12
  55.         :       :    :
  56. Consider the last value, that is BCD presentation of 12. The
  57. corresponding hexadecimal is $12 (not $C as in normal decimal to
  58. hexadecimal conversion). The crucial question is how to convert
  59. 12BCD to $12 (or its normal decimal equivalent 18). Here is my
  60. sample code:
  61.   type BCDType = array [0..7] of char;
  62.   {}
  63.   procedure StrToBCD (s : string; var b : BCDType);
  64.   var i, p : byte;
  65.   begin
  66.     FillChar(b, SizeOf(b), '0');
  67.     p := Length (s);
  68.     if p > 8 then exit;
  69.     for i := p downto 1 do b[p-i] := s[i];
  70.   end;  (* strtobcd *)
  71.   {}
  72.   function BCDtoDec (b : BCDType; var ok : boolean) : longint;
  73.   const Digit : array [0..9] of char = '0123456789';
  74.   var i, k : byte;
  75.       y, d : longint;
  76.   begin
  77.     y := 0;
  78.     d := 1;
  79.     ok := false;
  80.     for i := 0 to 7 do begin
  81.       k := Pos (b[i], Digit);
  82.       if k = 0 then exit;
  83.       y := y + (k-1) * d;
  84.       if i < 7 then d := 16 * d;
  85.     end; { for }
  86.     ok := true;
  87.     BCDtoDec := y;
  88.   end;  (* bcdtodec *)
  89.   {}
  90.   {}
  91.   procedure TEST;
  92.   var i  : byte;
  93.       b  : BCDType;
  94.       x  : longint;
  95.       ok : boolean;
  96.       s  : string;
  97.   begin
  98.     s := '12';
  99.     StrToBCD (s, b);
  100.     write ('The BCD value : ');
  101.     for i := 7 downto 0 do write (b[i], ' ');
  102.     writeln;
  103.     x := BCDtoDec (b, ok);
  104.     if ok then writeln ('is ', x, ' as an ordinary decimal')
  105.       else writeln ('Error in BCD');
  106.   end;  (* test *)
  107.   {}
  108.   begin TEST; end.
  109.  
  110. Next we can ask, what if the BCD value is given as an integer.
  111. Simple, first convert the integer into a string. For example in
  112. the procedure TEST put
  113.   Str (12, s);
  114.  
  115. Finally, what about converting an ordinary decimal to the
  116. corresponding BCD but given also as a decimal variable.  For example
  117. 18 --> 12?
  118.   function LHEXFN (decimal : longint) : string;
  119.   const hexDigit : array [0..15] of char = '0123456789ABCDEF';
  120.   var i : byte;
  121.       s : string;
  122.   begin
  123.     FillChar (s, SizeOf(s), ' ');
  124.     s[0] := chr(8);
  125.     for i := 0 to 7 do
  126.       s[8-i] := HexDigit[(decimal shr (4*i)) and $0F];
  127.     lhexfn := s;
  128.   end;  (* lhexfn *)
  129.   {}
  130.   function DecToBCD (x : longint; var ok : boolean) : longint;
  131.   const Digit : array [0..9] of char = '0123456789';
  132.   var hexStr : string;
  133.   var i, k : byte;
  134.       y, d : longint;
  135.   begin
  136.     hexStr := LHEXFN(x);
  137.     y := 0;
  138.     d := 1;
  139.     ok := false;
  140.     for i := 7 downto 0 do begin
  141.       k := Pos (hexStr[i+1], Digit);
  142.       if k = 0 then exit;
  143.       y := y + (k-1) * d;
  144.       if i > 0 then d := 10 * d;
  145.     end; { for }
  146.     ok := true;
  147.     DecToBCD := y;
  148.   end;  (* dectobcd *)
  149.   {}
  150.   procedure TEST2;
  151.   var i    : byte;
  152.       x10  : longint;
  153.       xBCD : longint;
  154.       ok   : boolean;
  155.   begin
  156.     x10 := 18;
  157.     writeln ('The ordinary decimal value : ', x10);
  158.     xBCD := DecToBCD (x10, ok);
  159.     if ok then writeln ('is ', xBCD, ' as a binary coded decimal')
  160.       else writeln ('Error in BCD');
  161.   end;  (* test2 *)
  162.   {}
  163.   begin TEST; end.
  164. -------------------------------------------------------------------
  165.  
  166. From ts@uwasa.fi Sun Dec 26 00:01:02 1993
  167. Subject: Copying with TP
  168.  
  169. 62. *****
  170.  Q: How can I copy a file in a Turbo Pascal program?
  171.  
  172.  A: Here is the code. Take a close look. It has some instructive
  173. features besides the copying, like handling the filemode and using
  174. dynamic variables (using pointers).
  175.   procedure SAFECOPY (fromFile, toFile : string);
  176.   type bufferType = array [1..65535] of char;
  177.   type bufferTypePtr = ^bufferType;  { Use the heap }
  178.   var bufferPtr : bufferTypePtr;     { for the buffer }
  179.       f1, f2 : file;
  180.       bufferSize, readCount, writeCount : word;
  181.       fmSave : byte;              { To store the filemode }
  182.   begin
  183.     bufferSize := SizeOf(bufferType);
  184.     if MaxAvail < bufferSize then exit;  { Assure there is enough memory }
  185.     New (bufferPtr);              { Create the buffer }
  186.     fmSave := FileMode;           { Store the filemode }
  187.     FileMode := 0;                { To read also read-only files }
  188.     Assign (f1, fromFile);
  189.     {$I-} Reset (f1, 1); {$I+}    { Note the record size 1, important! }
  190.     if IOResult <> 0 then exit;   { Does the file exist? }
  191.     Assign (f2, toFile);
  192.     {$I-} Reset (f2, 1); {$I+}    { Don't copy on an existing file }
  193.     if IOResult = 0 then begin close (f2); exit; end;
  194.     {$I-} Rewrite (f2, 1); {$I+}  { Open the target }
  195.     if IOResult <> 0 then exit;
  196.     repeat                        { Do the copying }
  197.       BlockRead (f1, bufferPtr^, bufferSize, readCount);
  198.       {$I-} BlockWrite (f2, bufferPtr^, readCount, writeCount); {$I+}
  199.       if IOResult <> 0 then begin close (f1); exit; end;
  200.     until (readCount = 0) or (writeCount <> readCount);
  201.     writeln ('Copied ', fromFile, ' to ', toFile,
  202.              ' ', FileSize(f2), ' bytes');
  203.     close (f1); close (f2);
  204.     FileMode := fmSave;           { Restore the original filemode }
  205.     Dispose (bufferPtr);          { Release the buffer from the heap }
  206.   end;  (* safecopy *)
  207.  
  208. Of course a trivial solution would be to invoke the MsDos copy
  209. command using the Exec routine. (See the item "How do I execute an
  210. MsDos command from within a TP program?"
  211. ------------------------------------------------------------------
  212.  
  213. From ts@uwasa.fi Sun Dec 26 00:01:03 1993
  214. Subject: C modules in TP
  215.  
  216. 63. *****
  217.  Q: How can I use C code in my Turbo Pascal program?
  218.  
  219.  A: I have very little information on this question, since I do not
  220. program in C myself.  However in reading Turbo Pascal textbooks I
  221. have come across a couple of references I can give.  They are Edward
  222. Mitchell (1993), Borland Pascal Developer's Guide, pp. 60-64, and
  223. Stoker & Ohlsen (1989), Turbo Pascal Advanced Techniques, Ch 4.
  224. ------------------------------------------------------------------
  225.  
  226. From ts@uwasa.fi Sun Dec 26 00:01:04 1993
  227. Subject: Using Turbo Profiler
  228.  
  229. 64. *****
  230.  Q: How do I get started with the Turbo Profiler?
  231.  
  232.  A: Borland's separate Turbo Profiler is a powerful tool for
  233. improving program code and enhancing program performance, but far
  234. from an easy to use. It is an advanced tool. In fact setting it up
  235. the first time is almost a kind of detective work.
  236.    Let's walk through the steps with Turbo Profiler version 1.01 to
  237. see where a running Turbo Pascal program takes its time.
  238. Assume a working directory r:\
  239. 1. Copy the target .PAS file to r:\
  240. 2. Compile it with TURBO.EXE using the following Compiler and
  241.    Debugger options. The standalone debugging option is crucial.
  242.      Code generation
  243.       [ ] Force far calls        [X] Word align data
  244.       [ ] Overlays allowed       [ ] 286 instructions
  245.      Runtime errors             Syntax options
  246.       [ ] Range checking         [X] Strict var-strings
  247.       [X] Stack checking         [ ] Complete boolean eval
  248.       [ ] I/O checking           [X] Extended syntax
  249.       [ ] Overflow checking      [ ] Typed @ operator
  250.                                  [ ] Open parameters
  251.      Debugging
  252.       [X] Debug information     Numeric processing
  253.       [X] Local symbols          [ ] 8087/80287
  254.                                  [ ] Emulation
  255.      Debugging         Display swapping
  256.       [X] Integrated    ( ) None
  257.       [X] Standalone    () Smart
  258.                         ( ) Always
  259. 3) Call TPROF.EXE
  260. 4) Load the .EXE file produced by compilation in item 2.
  261. 5) Choose from the TPROF menus
  262.      Statistics
  263.        Profiling options...
  264.          Profile mode
  265.           () Active    ( ) Passive
  266.           Run count
  267.            1
  268.           Maximum areas
  269.            200
  270. 6) Choose from the TPROF menus
  271.       Options
  272.        Save options...
  273.       [X] Options
  274.       [ ] Layout
  275.       [ ] Macros
  276.      Save To
  277.       r:\tfconfig.tf
  278. 7) Press Alt-F10 for the Local Menu. Choose
  279.      Add areas
  280.        All routines
  281. and so on.
  282. 8) Choose Run from the TPROF menus (or F9)
  283. 9) Choose from the TPROF menus
  284.       Print
  285.        Options...
  286.      Width
  287.       80
  288.      Height
  289.       9999
  290.       ( ) Printer     ( ) Graphics
  291.       () File        () ASCII
  292.      Destination File
  293.       r:\report.lst
  294. 10) Print
  295.        Module...
  296.          All modules
  297.          Statistics
  298.            Overwrite
  299. Also see Edward Mitchell (1993), Borland Pascal Developer's Guide.
  300. It has a a very instructive chapter "Program Optimization" on the
  301. Turbo Profiler. The material in the Turbo Profiler manual is so
  302. complicated that additional guidance like Mitchell's is very much
  303. needed.
  304. ------------------------------------------------------------------
  305.